package com.ruoyi.eam.service.impl;

import java.util.List;

import com.ruoyi.eam.doman.EamAsset;
import com.ruoyi.eam.doman.EamLoanbackD;
import com.ruoyi.eam.mapper.EamAllotHMapper;
import com.ruoyi.eam.mapper.EamAssetMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.eam.mapper.EamAllotDMapper;
import com.ruoyi.eam.doman.EamAllotD;
import com.ruoyi.eam.service.IEamAllotDService;

/**
 * 资产调拨从Service业务层处理
 *
 * @author xl
 * @date 2024-07-12
 */
@Service
public class EamAllotDServiceImpl implements IEamAllotDService
{
    @Autowired
    private EamAllotDMapper eamAllotDMapper;
    @Autowired
    private EamAllotHMapper allotHMapper;
    @Autowired
    private EamAssetMapper eamAssetMapper;

    /**
     * 查询资产调拨从
     *
     * @param assetId 资产调拨从主键
     * @return 资产调拨从
     */
    @Override
    public EamAllotD selectEamAllotDByAssetId(Long assetId)
    {
        return eamAllotDMapper.selectEamAllotDByAssetId(assetId);
    }

    /**
     * 查询资产调拨从列表
     *
     * @param eamAllotD 资产调拨从
     * @return 资产调拨从
     */
    @Override
    public List<EamAllotD> selectEamAllotDList(EamAllotD eamAllotD)
    {
        return eamAllotDMapper.selectEamAllotDList(eamAllotD);
    }

    /**
     * 新增资产调拨从
     *
     * @param eamAllotD 资产调拨从
     * @return 结果
     */
    @Override
    public int insertEamAllotD(EamAllotD eamAllotD)
    {
        return eamAllotDMapper.insertEamAllotD(eamAllotD);
    }

    /**
     * 修改资产调拨从
     *
     * @param eamAllotD 资产调拨从
     * @return 结果
     */
    @Override
    public int updateEamAllotD(EamAllotD eamAllotD)
    {
        return eamAllotDMapper.updateEamAllotD(eamAllotD);
    }

    /**
     * 批量删除资产调拨从
     *
     * @param assetIds 需要删除的资产调拨从主键
     * @return 结果
     */
    @Override
    public int deleteEamAllotDByAssetIds(Long[] assetIds)
    {
        return eamAllotDMapper.deleteEamAllotDByAssetIds(assetIds);
    }

    /**
     * 删除资产调拨从信息
     *
     * @param assetId 资产调拨从主键
     * @return 结果
     */
    @Override
    public int deleteEamAllotDByAssetId(Long assetId)
    {
        return eamAllotDMapper.deleteEamAllotDByAssetId(assetId);
    }

    @Override
    public int inertAllotD(List<EamAsset> list,Long allotId) {
        //获取最后一条变更id
        Long allotIdLast = allotHMapper.queryEamAllotHList().get(allotHMapper.queryEamAllotHList().size() - 1).getAllotId();
        if(allotId != null){        //修改
            allotIdLast = allotId;
        }
        System.out.println("allotIdLast:"+allotIdLast);
        for (EamAsset asset : list) {
            System.out.println(123);
            EamAllotD eamChgD = new EamAllotD();
            eamChgD.setAllotId(allotIdLast);
            eamChgD.setAssetId(asset.getAssetId());
            eamChgD.setCount(asset.getAssetNum());
            //添加变更从表信息
            int num = eamAllotDMapper.insertEamAllotD(eamChgD);
            //修改资产信息
            int num1 = eamAssetMapper.updateEamAsset(asset);
            if(num == 0 || num1 == 0){
                return 0;
            }
        }
        return 1;
    }
}
